local _M = {}
local nut = _M

local uuid = require 'resty.jit-uuid'
local json = require 'cjson.safe'
local utils = require 'utils'

_M.Nut = {}

function _M.Nut:new(obj)
    obj = obj or {}
    local lself = {}
    setmetatable(obj, lself)
    lself.__index = lself

    lself.sid = string.sub(string.gsub(uuid(), '-', ''), 1, 20) .. string.sub(uuid(), 25, 36)
    lself.stime = ngx.now() * 1000
    lself.tcost = nil

    -- http request initial params
    lself.http_req = {
        real_ip = ngx.var.remote_addr,
        port = ngx.var.server_port,
        ua = ngx.var.http_user_agent,
        headers = ngx.req.get_headers(),
        method = ngx.var.request_method,
        path = ngx.var.document_uri,
        query_args = ngx.req.get_query_args(),
        body = ngx.req.get_body_data(),
        hostname = ngx.var.hostname,
    }

    local header_data = lself.http_req.headers['x-forwarded-for']
    if header_data ~= nil then
        if type(header_data) == 'table' then
            dlog.error('header_data is table!! '..json.encode(header_data))
        else
            lself.http_req.real_ip = utils.strSplit(header_data, ',')[1]
        end
    end

    -- http response
    lself.http_rsp = {
        code = ngx.HTTP_OK,
        body = ''
    }

    return obj
end

return nut
